﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MyMainSite.Models;

namespace MyMainSite.Repositories
{
    public static class MetaWeblogImageRepository
    {
        #region Public Static Methods

        public static int Add(string name, byte[] data, string mimeType)
        {
            var existingImage = MetaWeblogImageRepository.GetByName(name);

            if (existingImage == null)
            {
                RichardsSiteEntities context = new RichardsSiteEntities();

                MetaWeblogImage image = new MetaWeblogImage();
                image.Data = data;
                image.Name = name;
                image.MIMEType = mimeType;

                context.MetaWeblogImages.AddObject(image);

                context.SaveChanges();

                return image.MetaWeblogImageId;
            }
            else
            {
                return existingImage.MetaWeblogImageId;
            }
        }

        public static MetaWeblogImage GetByName(string name)
        {
            RichardsSiteEntities context = new RichardsSiteEntities();

            name = name.ToLower();

            var image = (from i in context.MetaWeblogImages
                         where i.Name.ToLower() == name
                         select i).FirstOrDefault();

            return image;
        }

        #endregion
    }
}